package net.dipper.tankwar.editor.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import wnb.util.io.ImageManager;
import wnb.util.ui.AnimationHelper;

import net.dipper.tankwar.core.config.ConfigItem;

public class ObjectListItem extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ConfigItem item;
	private ArrayList<ActionListener> listeners;
	private JLabel icon;
	private JLabel label;
	private JButton btn;
	
	public ObjectListItem(ObjectList parent, ConfigItem item){
		this.item = item;
		this.listeners = new ArrayList<ActionListener>();
		
		ImageIcon imgIcon = new ImageIcon();
		try{
			Image img = ImageManager.getImage(item.getImg());
			img = AnimationHelper.scale(img, 0.5, 0.5);
			imgIcon.setImage(img);
		}catch(IOException ex){
			ex.printStackTrace();
		}
		
		this.icon = new JLabel(imgIcon);
		this.label = new JLabel(item.getName());
		this.btn = new JButton("+");
		this.btn.addActionListener(this);
		
		this.icon.setPreferredSize(new Dimension(32, 32));
		this.label.setForeground(new Color(130, 130, 200));
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		add(this.icon);
		add(Box.createHorizontalGlue());
		add(this.label);
		add(Box.createHorizontalGlue());
		add(this.btn);
		add(Box.createRigidArea(new Dimension(10, 10)));
	}
	
	public void addActionListener(ActionListener listener){
		this.listeners.add(listener);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source.equals(btn)){
			e.setSource(this.item);
			for(ActionListener listener : listeners){
				listener.actionPerformed(e);
			}
		}
	}
}
